package middleware

import (
	"egin/app/service"
	"egin/pkg/response"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

// 判断是否未登录
func RbacCheck() gin.HandlerFunc {
	return func(c *gin.Context) {

		res := service.RbacCheck(c)
		if !res {
			XMLHttpRequest := c.GetHeader("X-Requested-With")
			if XMLHttpRequest == "XMLHttpRequest" {
				//ajax
				response.ErrorWithDetailed(403, "", "您没有权限", c)
				c.Abort()

			} else {
				ContentType := c.GetHeader("Content-Type")
				if strings.Contains(ContentType, "json") {
					response.ErrorWithDetailed(403, "", "您没有权限", c)
				} else {
					c.HTML(http.StatusOK, "errors/403.tmpl", nil)
				}

				//c.Redirect(http.StatusTemporaryRedirect, "/login")
				c.Abort()

			}
		}

		//c.Abort()

		// 处理请求
		c.Next()
	}
}
